<?php
class Admin_SortsController extends My_Controllers_Admin {
	public function indexAction() {
		$db = Zend_Registry::get ( 'db' );
		$select = new Zend_Db_Select ( $db );
		$select->from ( array ('l' => 'bc_lanmu' ), array ('id', 'name', 'kid' ) );
		$select->joinLeft ( array ('n' => 'bc_news' ), 'l.id = n.column_id', array ('c' => 'COUNT(*)' ) );
		$select->joinLeft ( array ('k' => 'bc_keshi' ), 'l.kid = k.id', array ('kename' ) );
		$select->group ( 'l.id' );
		$auth = Zend_Auth::getInstance ();
		if ($auth->getIdentity ()->group != 'admin') {
			$select->where ( 'k.id = ?', $this->ke_id );
		}
		$sql = $select->__toString ();
		$d = $db->fetchAll ( $sql );
		$arr = new My_Helper_Array ( );
		$this->view->d = $arr->array_group_by ( $d, 'kid' );
		//		Zend_Debug::dump ( $sql );
	//		Zend_Debug::dump ( $d );
	}
	public function msgAction() {
		if ($this->_getParam ( 'id' )) {
			$action = 'updata';
		} else {
			$action = 'insert';
		}
		$kid = $this->_getParam ( 'kid' );
		$sname = $this->_getParam ( 'sname' );
		$url = $this->view->url ( array ('action' => $action ) );
		$form = new My_Forms_Admin_Sorts ( $kid, $sname, $action, $url );
		$this->view->form = $form;
	}
	public function insertAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		$form = new My_Forms_Admin_Sorts ( );
		$formData = $this->_request->getPost ();
		
		if ($form->isValid ( $formData )) {
			array_pop ( $formData );
			Zend_Debug::dump ( $formData );
			$lanmu = new Lanmu ( );
			
			try {
				$lanmu->insert ( $formData );
				$this->_redirect ( '/admin/sorts' );
			} catch ( PDOException $e ) {
				Zend_Debug::dump ( $e->getMessage () );
			}
		} else {
			echo ("no");
		}
	}
	public function updataAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		$form = new My_Forms_Admin_Sorts ( );
		$formData = $this->_request->getPost ();
		
		if ($form->isValid ( $formData )) {
			Zend_Debug::dump ( $formData );
			try {
				$lanmu = new Lanmu ( );
				$l = $lanmu->find ( $this->_getParam ( 'id' ) )->current ();
				$l->name = $formData ['name'];
				$l->kid = $formData ['kid'];
				$l->save ();
				$this->_redirect ( '/admin/sorts' );
			} catch ( PDOException $e ) {
				Zend_Debug::dump ( $e->getMessage () );
			}
		} else {
			echo ("no");
		}
	}

}
